package com.zzyl.service.impl;

import com.zzyl.constant.Constants;
import com.zzyl.dto.LoginDto;
import com.zzyl.mapper.LoginMapper;
import com.zzyl.properties.JwtTokenManagerProperties;
import com.zzyl.service.LoginService;
import com.zzyl.utils.JwtUtil;
import com.zzyl.vo.LoginVo;
import com.zzyl.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @Description LoginServiceImpl
 * @Author Wen Jie
 * @Date 2024-09-09
 */
@Service
public class LoginServiceImpl implements LoginService {
    @Autowired
    private LoginMapper loginMapper;
    @Autowired
    private JwtTokenManagerProperties jwtTokenManagerProperties;
    @Override
    public UserVo login(LoginDto loginDto) {
        int count = loginMapper.login(loginDto);
        if (count == 0){
            throw new RuntimeException("用户名或密码错误");
        }
        UserVo userVo= loginMapper.getInfo(loginDto);
        if(userVo != null){
            Map<String,Object> map = new HashMap<>();
            map.put("userId",userVo.getId());
            map.put("userName", userVo.getRealName());
            String jwt = JwtUtil.createJWT(jwtTokenManagerProperties.getBase64EncodedSecretKey() + "123", 3600*1000, map);
            userVo.setUserToken(jwt);
            return userVo;
        }
        return null;
    }
}
